import entity.Employee;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * Stream的操作步骤：创建Stream、中间操作、终止操作
 * Created by VincentZee on 2019/3/5
 */
public class TestStreamAPI01 {
    List<Employee> emps = Arrays.asList(
            new Employee(102, "李四", 59, 6666.66),
            new Employee(101, "张三", 18, 9999.99),
            new Employee(103, "王五", 28, 3333.33),
            new Employee(104, "赵六", 8, 7777.77),
            new Employee(104, "赵六", 8, 7777.77),
            new Employee(104, "赵六", 8, 7777.77),
            new Employee(105, "田七", 38, 5555.55)
    );

    /*
		中间操作：映射
		map——接收Lambda，将元素转换成其他形式或提取信息。接收一个函数作为参数，该函数会被应用到每个元素上，并将其映射成一个新的元素。
		flatMap——接收一个函数作为参数，将流中的每个值都换成另一个流，然后把所有流连接成一个流
	 */
    @Test
    public void test01() {
        Stream<String> stringStream = emps.stream().map(Employee::getName);

        List<String> stringList = Arrays.asList("aaa", "bbb", "ccc", "ddd");
        Stream<String> stream = stringList.stream().map(String::toUpperCase);
        stream.forEach(System.out::println);

        Stream<Stream<Character>> stream1 = stringList.stream().map(TestStreamAPI01::filterCharacter);
        stream1.forEach((sm) -> {
            sm.forEach(System.out::println);
        });

        Stream<Character> stream2 = stringList.stream().flatMap(TestStreamAPI01::filterCharacter);
        stream2.forEach(System.out::println);
    }

    public static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();
        for (Character ch : str.toCharArray()) {
            list.add(ch);
        }
        return list.stream();
    }

    /*
        sorted：自然顺序排序
        sorted(Comparator com)：定制顺序排序
     */
    @Test
    public void test02() {
        emps.stream().map(Employee::getName).sorted().forEach(System.out::println);

        emps.stream().sorted((x, y) -> {
            if (x.getAge() == y.getAge()) {
                return x.getName().compareTo(y.getName());
            } else {
                return Integer.compare(x.getAge(), y.getAge());
            }
        }).forEach(System.out::println);
    }
}
